package com.example.hsh.homefragment.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.example.hsh.homefragment.R;
import com.example.hsh.homefragment.adapter.MainFragmentPagerAdapter;
import com.example.hsh.homefragment.rx.model.WXItemBean;
import com.example.hsh.homefragment.rx.presenter.RxBasePresenter;
import com.example.hsh.homefragment.rx.presenter.WeChatPresenter;
import com.example.hsh.homefragment.rx.view.WeChatNewView;
import com.example.hsh.homefragment.utils.ScreenUtil;
import com.example.hsh.homefragment.view.StickyNavLayout;

import java.util.ArrayList;

import butterknife.BindView;


/**
 * Created by HSH on 2016/12/13.
 */

public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener,WeChatNewView {

    private ArrayList<String> datas = new ArrayList<>();
    private MainFragmentCallBack callBack;

    @BindView(R.id.tl_tabs)
    TabLayout tabs;

    @BindView(R.id.vp_main)
    ViewPager vpMain;

    @BindView(R.id.sf_layout)
    SwipeRefreshLayout sf_layout;

    @BindView(R.id.nested_layout)
    StickyNavLayout nested_layout;

    public static MainFragment newInstance(boolean showToolbar) {
        Bundle args = new Bundle();
        args.putBoolean(SHWO_TOOL_BAR, showToolbar);
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void iniView(View rootView, @Nullable Bundle savedInstanceState) {

        sf_layout.setColorSchemeResources(R.color.second_degree_title_text_blue, R.color.colorPrimary_blue, R.color.colorPrimaryDark_blue);
        sf_layout.setSize(SwipeRefreshLayout.LARGE);
        sf_layout.setOnRefreshListener(this::onRefresh);

        sf_layout.setProgressViewOffset(false, (int) (-20 * ScreenUtil.getDensity(mContext)), (int) (70 * ScreenUtil.getDensity(mContext)));

        tabs.addOnTabSelectedListener(this);

        vpMain.setAdapter(new MainFragmentPagerAdapter(getChildFragmentManager()));
        tabs.setupWithViewPager(vpMain);

        if (toolbar != null) {
            toolbar.setNavigationIcon(R.mipmap.ic_menu_white_24dp);
        }
    }

    @Override
    protected void onNavigationClick(View view) {
        nested_layout.toggleRemainHeader(true);
    }

    private void onRefresh() {
        new Handler().postDelayed(() -> sf_layout.setRefreshing(false), 3000);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            getChildFragmentManager().beginTransaction()
                    .add(R.id.top_panel, TopPanelFragment.newInstance(), TopPanelFragment.class.getName()).commit();
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.layout_of_main_fragment;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        if (callBack != null) {
            callBack.addContentListFragment();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainFragmentCallBack) {
            callBack = (MainFragmentCallBack) context;
        }
    }

    @Override
    protected RxBasePresenter createPresenter() {
        return new WeChatPresenter();
    }

    @Override
    public void onGetHotNewListSuccess(WXItemBean wxItemBean) {

    }

    @Override
    public void onGetHotNewListFail(WXItemBean wxItemBean) {

    }

    @Override
    public Context getPresenterContext() {
        return mContext;
    }

    public interface MainFragmentCallBack {
        void addTopPanelFragment();

        void addContentListFragment();
    }
}
